﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.Blog.Helper;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Blog.Components.Extensions;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Blog.Managers;
using MvcEngine.Core.Exceptions;
using MvcEngine.Core;

namespace MvcEngine.Blog.StarterSite.Filters
{
    public class BlogActionFilter : IActionFilter, IResultFilter
    {
        //TODO: Cache results
        //Should be called only once if module has been installed
#warning should be called only for blog module

        public void OnResultExecuting(ResultExecutingContext filterContext)
        {
            MvcEngineModel model = filterContext.Controller.ViewData.Model as MvcEngineModel;
            if (model == null) return;

            if (filterContext.RouteData.Values["pageName"] == null)
                return;

            BlogData blog = BlogManager.Current.GetCurrentBlog();
            if (blog != null)
            {
                model.SetModelValue("BlogDescription", blog.BlogDescription);
                model.SetModelValue("BlogName", blog.BlogName);
                model.SetModelValue("HtmlHeader", blog.HtmlHeader);

                UrlHelper helper = new UrlHelper(filterContext.RequestContext);

                model.SetModelValue("BlogRootLink", helper.Page(filterContext.RouteData.Values["pageName"].ToString()));
            }
        }

        public void OnResultExecuted(ResultExecutedContext filterContext)
        {

        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {

        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            BlogData blog = null;

            string blogName = (string)filterContext.RequestContext.RouteData.Values["blogName"];
            if (!string.IsNullOrEmpty(blogName))
            {
                blog = BlogManager.Current.GetBlog(blogName);
            }

            if (blog == null && filterContext.RequestContext.RouteData.Values["blogId"] != null)
            {
                string blogId = filterContext.RequestContext.RouteData.Values["blogId"].ToString();
                if (!string.IsNullOrEmpty(blogId))
                {
                    blog = BlogManager.Current.GetBlog(new Guid(blogId));
                }
            }

            if (blog == null)
                blog = BlogManager.Current.GetCurrentBlog();

            if (blog != null)
            {
                filterContext.ActionParameters["blogId"] = blog.Id;
                filterContext.ActionParameters["blogName"] = blog.Name;
                filterContext.RequestContext.RouteData.Values["blogId"] = blog.Id;
                filterContext.RequestContext.RouteData.Values["blogName"] = blog.Name;
            }


#warning refactor exception
            //else
            //{
            //    throw new BusinessException(string.Format("Blog module is not present on page: {0}", SiteContext.Current.CurrentPage.Name));
            //}
        }
    }
}
